package com.addplus.townmall.server.queue.serviceimpl;

import cn.binarywang.tools.generator.bank.BankCardNumberGenerator;
import com.addplus.townmall.server.api.mapper.town.GiftCardMapper;
import com.addplus.townmall.server.api.mapper.town.GiftMemberCardMapper;
import com.addplus.townmall.server.api.mapper.town.OrderItemsMapper;
import com.addplus.townmall.server.api.mapper.town.OrderMapper;
import com.addplus.townmall.server.api.model.town.GiftCard;
import com.addplus.townmall.server.api.model.town.GiftMemberCard;
import com.addplus.townmall.server.api.model.town.Order;
import com.addplus.townmall.server.api.model.town.OrderItems;
import com.addplus.townmall.server.api.model.town.expand.VerificationCode;
import com.addplus.townmall.server.api.service.queue.AsyncService;
import com.addplus.townmall.server.api.service.queue.GiftCardCreateService;
import com.addplus.townmall.server.api.utils.DataUtils;
import com.addplus.townmall.server.api.utils.VerificationCodeUtil;
import com.alibaba.dubbo.config.annotation.Service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;

/**
 * 类名: GiftCardCreateServiceImpl
 *
 * @author zhangjiehang
 * @version V1.0
 * @date 2018/11/7 3:06 AM
 * @description 类描述: 产生对应会员礼品卡信息
 */
@Service(interfaceClass = GiftCardCreateService.class, async = true, executes = 1)
public class GiftCardCreateServiceImpl implements GiftCardCreateService {

    private Logger logger = LoggerFactory.getLogger(GiftCardCreateServiceImpl.class);

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private OrderItemsMapper orderItemsMapper;

    @Autowired
    private GiftMemberCardMapper giftMemberCardMapper;

    @Autowired
    private GiftCardMapper giftCardMapper;

    @Override
    public void giftCardMemberCreate(String orderNum) {
        if (DataUtils.isEmpty(orderNum)) {
            logger.error("创建礼品卡，查询不到对应的订单号：" + orderNum);
            return;
        }
        QueryWrapper<Order> orderQueryWrapper = new QueryWrapper<>();
        orderQueryWrapper.eq("order_number", orderNum);
        orderQueryWrapper.eq("order_product_type", 2);
        orderQueryWrapper.eq("is_delete", 0);
        Order order = orderMapper.selectOne(orderQueryWrapper);
        if (order == null) {
            logger.error("创建礼品卡，查询不到对应的订单号：" + orderNum);
            return;
        }
        // 判断是否已经支付
        if (order.getOrderStatus() == 0 || order.getOrderStatus() == 6) {
            logger.error("创建礼品卡，当前订单不是正常状态，订单主键：" + order.getId() + ",状态：" + order.getOrderStatus());
            return;
        }
        // 判断当前礼品卡是否已经创建了
        QueryWrapper<GiftMemberCard> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("order_id", order.getId());
        queryWrapper.eq("is_delete", 0);
        int countGiftMemberCard = giftMemberCardMapper.selectCount(queryWrapper);
        if (countGiftMemberCard >= 1) {
            logger.error("创建礼品卡，当前订单已经创建礼品卡了，订单主键：" + order.getId() + ",已创建数量：" + countGiftMemberCard);
            return;
        }

        QueryWrapper<OrderItems> itemsQueryWrapper = new QueryWrapper<>();
        itemsQueryWrapper.eq("order_id", order.getId());
        itemsQueryWrapper.eq("is_delete", 0);
        List<OrderItems> orderItemsList = orderItemsMapper.selectList(itemsQueryWrapper);
        if (orderItemsList == null || orderItemsList.isEmpty()) {
            logger.error("创建礼品卡，当前订单查询不到对应订单子项，订单主键：" + order.getId());
        }
        final Date date = new Date();
        // 开始创建礼品卡
        orderItemsList.forEach(orderItems -> {
            // 查询对应礼品卡
            GiftCard giftCard = giftCardMapper.selectById(orderItems.getShopProductId());
            if (giftCard != null) {
                //插入数量
                for (int i = 0; i < orderItems.getPurchaseQuantity(); i++) {
                    GiftMemberCard giftMemberCard = new GiftMemberCard();
                    giftMemberCard.setOrderId(order.getId());
                    giftMemberCard.setMemberId(order.getMemberId());
                    giftMemberCard.setGiftStatus(0);
                    giftMemberCard.setGiftCardName(giftCard.getGiftCardName());
                    giftMemberCard.setGiftCardImg(giftCard.getGiftCardSealImg());
                    giftMemberCard.setGiftValue(giftCard.getGiftValue());
                    giftMemberCard.setGiftValueLeft(giftCard.getGiftValue());
                    giftMemberCard.setIsDelete(0);
                    giftMemberCard.setGmtCreate(date);
                    giftMemberCard.setGmtModified(date);
                    giftMemberCardMapper.insert(giftMemberCard);
                }
            }
        });
        //更新订单状态
        Order orderNew = new Order();
        orderNew.setId(order.getId());
        orderNew.setGmtModified(date);
        orderNew.setOrderStatus(3);
        orderMapper.updateById(orderNew);
    }
}
